/*
    Copyright (C) 2025 mio <stigma@disroot.org>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "aster_controlbar.h"

#include <tqlayout.h>
#include <tqtoolbutton.h>

#include <kiconloader.h>

#include "aster_slider.h"
#include "playback_state.h"

ControlBar::ControlBar(TQWidget *parent, const char *name)
    : TQWidget(parent, name)
    , m_playerState(StoppedState)
{
    TQVBoxLayout *vlayout = new TQVBoxLayout(this, 4, 4);

    m_slider = new AsterSlider(this);
    m_slider->setOrientation(TQt::Horizontal);
    m_slider->setTickmarks(TQSlider::NoMarks);
    m_slider->setMinValue(0);
    m_slider->setMaxValue(0);

    vlayout->addWidget(m_slider);

    TQHBoxLayout *buttonBox = new TQHBoxLayout(vlayout, 0);

    m_playButton = new TQToolButton(this);
    m_playButton->setIconSet(MainBarIcon("media-playback-start"));

    TQToolButton *stopButton = new TQToolButton(this);
    stopButton->setIconSet(MainBarIconSet("media-playback-stop"));

    TQToolButton *prevButton = new TQToolButton(this);
    prevButton->setIconSet(MainBarIconSet("media-skip-backward"));

    TQToolButton *nextButton = new TQToolButton(this);
    nextButton->setIconSet(MainBarIconSet("media-skip-forward"));

    m_muteButton = new TQToolButton(this);
    m_muteButton->setIconSet(MainBarIconSet("audio-volume-high"));
    m_muteButton->setToggleButton(true);

    m_volume = new AsterSlider(this);
    m_volume->setOrientation(TQt::Horizontal);
    m_volume->setRange(0, 100);
    m_volume->setValue(100); // TODO: Save as a preference?

    m_playButton->setAutoRaise(true);
    stopButton->setAutoRaise(true);
    prevButton->setAutoRaise(true);
    nextButton->setAutoRaise(true);
    m_muteButton->setAutoRaise(true);

    buttonBox->addWidget(prevButton);
    buttonBox->addSpacing(4);
    buttonBox->addWidget(m_playButton);
    buttonBox->addWidget(stopButton);
    buttonBox->addSpacing(4);
    buttonBox->addWidget(nextButton);
    buttonBox->addStretch(1);
    buttonBox->addWidget(m_muteButton);
    buttonBox->addSpacing(4);
    buttonBox->addWidget(m_volume);

    connect(m_playButton, TQ_SIGNAL(clicked()), TQ_SLOT(playClicked()));
    connect(stopButton, TQ_SIGNAL(clicked()), TQ_SIGNAL(stop()));
    connect(prevButton, TQ_SIGNAL(clicked()), TQ_SIGNAL(previous()));
    connect(nextButton, TQ_SIGNAL(clicked()), TQ_SIGNAL(next()));
    connect(m_muteButton, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(muteChanged(bool)));

    connect(m_slider, TQ_SIGNAL(sliderMoved(int)), TQ_SIGNAL(seek(int)));
    connect(m_volume, TQ_SIGNAL(sliderMoved(int)), TQ_SIGNAL(volumeChanged(int)));

    connect(m_muteButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotMuteClicked()));
    connect(m_volume, TQ_SIGNAL(sliderMoved(int)), TQ_SLOT(slotVolumeChanged(int)));
}

void ControlBar::lengthChanged(int value)
{
    m_slider->setMaxValue(value);
}

void ControlBar::setState(int state)
{
    m_playerState = state;

    switch (state)
    {
        case PlayingState: {
            m_playButton->setIconSet(MainBarIconSet("media-playback-pause"));
            break;
        }
        case PausedState: {
            m_playButton->setIconSet(MainBarIconSet("media-playback-start"));
            break;
        }
        case StoppedState: {
            m_slider->setValue(0);
            m_slider->setMaxValue(0);
            m_playButton->setIconSet(MainBarIconSet("media-playback-start"));
            break;
        }
        default: {
            tqWarning("Unhandled state change in ControlBar: %d", state);
            break;
        }
    }
}

void ControlBar::tick(int64_t value)
{
    if (!m_slider->isHeld()) {
        m_slider->setValue(value);
    }
}

void ControlBar::playClicked()
{
    switch (m_playerState)
    {
        case PlayingState: {
            emit pause();
            break;
        }
        case StoppedState:
        case PausedState: {
            emit play();
            break;
        }
    }
}

void ControlBar::slotMuteClicked()
{
    if (m_muteButton->isOn()) {
        m_volume->setEnabled(false);
        m_muteButton->setIconSet(MainBarIconSet("audio-volume-muted"));
    } else {
        m_volume->setEnabled(true);
        slotVolumeChanged(m_volume->value());
    }
}

void ControlBar::slotVolumeChanged(int volume)
{
    if (volume == 0) {
        m_muteButton->setIconSet(MainBarIconSet("audio-volume-muted"));
    } else if (volume < 33) {
        m_muteButton->setIconSet(MainBarIconSet("audio-volume-low"));
    } else if (volume < 66) {
        m_muteButton->setIconSet(MainBarIconSet("audio-volume-medium"));
    } else {
        m_muteButton->setIconSet(MainBarIconSet("audio-volume-high"));
    }
}

#include "aster_controlbar.moc"

